package com.cartoonrabbit.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpStatus;
import com.cartoonrabbit.config.exception.ServiceException;
import com.cartoonrabbit.config.minio.MinioProperties;
import com.cartoonrabbit.config.minio.MinioUtils;
import com.cartoonrabbit.pojo.User;
import com.cartoonrabbit.mapper.UserMapper;
import com.cartoonrabbit.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cartoonrabbit.vo.RespBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LiuTeng
 * @since 2023-01-08
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MinioUtils minioUtils;
    @Autowired
    private MinioProperties minioProperties;


    /**
     * 获取用户信息
     */
    @Override
    public User getUserInfoByOpenId(String openId){
        try {

            User user = userMapper.selectById(openId);
            return user;
        }catch (Exception e){
            throw new ServiceException("服务器异常", HttpStatus.HTTP_INTERNAL_ERROR);
        }
    }

    /**
     * 保存用户信息
     * @param user
     * @return
     */
    @Override
    @Transactional
    public User saveUserInfo(User user) {
        if (ObjectUtils.isEmpty(user)){
            return null;
        }
        int flag=1;
        int flag2=1;
        try {
            flag = userMapper.insert(user);
        }catch (Exception e){
            throw new ServiceException("服务器异常", HttpStatus.HTTP_INTERNAL_ERROR);
        }
        if (flag>=1&&flag2>=1){
            return userMapper.selectById(user.getOpenId());
        }else {
            return null;
        }
    }

    /**
     * 修改用户名称
     * @param openId
     * @param openId
     * @return
     */
    @Override
    public RespBean updateUsername(String nickname, String openId) {
        User user = new User();
        user.setOpenId(openId);
        user.setNickName(nickname);
        int i = userMapper.updateById(user);

        if (i>0){
            return RespBean.success("修改成功",getUserInfoByOpenId(openId));
        }
        return RespBean.fail("服务器异常，请稍后再试");
    }

    /**
     * 用户头像修改
     * @param avatarFile
     * @param openId
     * @return
     */
    @Override
    public RespBean updateAvatar(MultipartFile avatarFile, String openId) {
        // 保存图片到minio
        MultipartFile[] multipartFiles=new MultipartFile[]{avatarFile};
        List<String> avatarList = minioUtils.upload(multipartFiles, minioProperties.getBucketName());
        if (avatarList==null||avatarList.size()==0){
            return RespBean.fail("服务器异常，上传失败");
        }
        // 获取路径
        User user = new User();
        user.setOpenId(openId);
        String avatar = minioProperties.getEndpoint() + "/" + avatarList.get(0);
        user.setAvatarUrl(avatar);

        int i = userMapper.updateById(user);
        if (i>0){
            return RespBean.success("修改成功",getUserInfoByOpenId(openId));
        }
        return RespBean.fail("服务器异常，请稍后再试");
    }

    @Override
    public RespBean productionChange(Integer times, String name) {
        User user = getById(name);
        user.setProductTimes(times+user.getProductTimes());
        updateById(user);
        return RespBean.success("SUCCESS",user);
    }

}
